﻿using System;

namespace dPet.Infrastructure.Logging.Implementation
{
	using Hangerd.Mongodb;

	public class OperatingLoggingService : IOperatingLoggingService
	{
		private const string OPERATINGLOG_DBNAME = "dPet_OperatingLog";
		private const string HOSPITAL_TABLENAME_FORMAT = "Hospital_{0}";

		private readonly IMongodbInsertService _mongodbInsertService;

		public OperatingLoggingService(IMongodbInsertService mongodbInsertService)
		{
			_mongodbInsertService = mongodbInsertService;
		}

		public void AddLog(IOperator @operator, string moduleName, string operatingName, string aggregateRootId, string remark, long cost)
		{
			if (@operator == null)
			{
				return;
			}

			var item = new MongodbItem
			{
				DatabaseName = OPERATINGLOG_DBNAME,
				TableName = string.Format(HOSPITAL_TABLENAME_FORMAT, @operator.HospitalId),
				Data = new OperatingLog
				{
					ApplicationName = string.Empty,//todo: ApplicationName
					OperatorLoginName = @operator.LoginName,
					ModuleName = moduleName,
					OperatingName = operatingName,
					AggregateRootId = aggregateRootId,
					Remark = remark,
					Cost = cost
				}
			};

			_mongodbInsertService.Insert(item);
		}
	}
}
